<?php

namespace app\api\controller;

use app\common\lib\ChatGpt;
use app\common\lib\Sensitive;
use app\common\model\MemberPointsLog;
use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\App;
use think\facade\View;
use think\facade\Db;
use think\response\Json;
/**
 * 文章标题
 *
 * api-ArticleTitle
 *
 * @Apidoc\Title("文章标题(会员)")
 * @Apidoc\Group("api")
 */
class ArticleTitleMember extends Auth
{
    /**
     * getAccess
     * @return Json|void
     *
     * @throws \DfaFilter\Exceptions\PdsBusinessException
     * @throws \DfaFilter\Exceptions\PdsSystemException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Apidoc\Title("获取能否请求")
     * @Apidoc\Url("/api/ArticleTitleMember/getAccess")
     * @Apidoc\Method("post")
     * @Returned("key", type="varchar",desc="用于获取标题的key")
     */
    public function getAccess()
    {
        /******************言论审查开始***********************************/
        //数据检验开始，这里审查用户的言论是否符合国家法律规定，这里一定要加上++++++++++++++++++++++++++++++++++++++++++++++++++++++
        $TextModeration=new \app\api\service\TextModerationService();
        if (!$TextModeration->check(json_encode($this->request->param(),JSON_UNESCAPED_UNICODE))) {
            return error($TextModeration->msg);
        }
        /*****************言论审查结束了**********************************/
        $key_word = input('key_word', 20, 'trim');
        $generated_quantity = input('generated_quantity', '', 'intval');

        if(!$key_word) {
            return error('关键词不能为空', []);
        }
        $res = Sensitive::has($key_word);
        if($res) {
            //return error('不能包含敏感词：'. Sensitive::get($key_word));
            return error('不能包含敏感词');
        }
        //查询距离上次生成标题的时间需要间隔在1分钟以上
        $lastTitle=Db::name("article_title")->where("member_id",$this->request->uid)->order("create_time desc")->find();
        if(time()-$lastTitle["create_time"]<20){
            return error('每次标题生成时间间隔需要大于20秒', []);
        }

        $point_per_title = config('user.point_per_title');
        if($point_per_title) {
            $member = \app\common\model\Member::find($this->request->uid);
            $member_point = $member['point_num'];
            //if($member_point < $point_per_title *  $generated_quantity) {
            if($member_point < 1) {
                return error('积分不足, 请充值积分！',['action' => 'recharge']);
            }else {
                $member->point_num = $member_point - $point_per_title;
                $member->save();

                MemberPointsLog::create([
                    'member_id' => $member->id,
                    'num' => -1 * $point_per_title ,
                    'end_num' => $member_point - 1,
                    'remarks' => "标题生成"
                ]);
            }
        }

        \app\common\model\ArticleTitle::create([
            'key_word' => $key_word,
            'generated_quantity' => $generated_quantity,
            'member_id' => $this->request->uid,
            'ip_address' => $this->request->ip()
        ]);

        $random_key = uniqid();
        cache($random_key, $key_word, ['expire' => 100]);
        return success('', ['key' => $random_key]);
    }
}
